package com.itheima.controller;

import com.itheima.common.JwtUtil;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户信息Controller
 * @Author: hsp
 * @Date: 2022/11/18-11-18-16:38
 * @Description: com.itheima.controller
 * @version: 1.0.0
 */
@RestController
@RequestMapping("/admin/user")
public class UserController {

    @Resource
    private UserService userService;

    // 登录
    @GetMapping("/login")
    public Map<String, Object> login(@RequestParam("username") String username,
                                     @RequestParam("password") String password){

        User user = userService.login(username, password);

        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(user)) {
            //创建token
            HashMap<String, Object> tokenMap = new HashMap<>();
            tokenMap.put("id", user.getId());

            //生成token令牌
            String token = JwtUtil.createToken(tokenMap);
            System.out.println("生成的token令牌是："+ token);

            //token令牌传到前端
            map.put("token", token);
            //登录成功
            map.put("code", "1");
        }else {
            //登录失败
            map.put("code", "0");
        }
        return map;
    }

}
